<?php
// +----------------------------------------------------------------------
// | IPHP
// +----------------------------------------------------------------------
// | This is an open source and simple PHP FrameWork.
// +----------------------------------------------------------------------
// | Author: wuzhuo
// +----------------------------------------------------------------------

// 文件上传类库

namespace IPHP\Lib;

class Upload {

    protected $formName;    //表单上传控件名称
    protected $maxSize;     //最大上传尺寸，单位mb,　默认２Ｍ
    protected $savePath;    //保存路径
    protected $error;       //错误信息
    protected $allowExt;    //允许的文件类型
    protected $info;        //上传的文件相关信息
    protected $ext;         //文件扩展名

    protected $filename;    //原始文件名
    protected $savename;    //保存的文件名
    protected $fullname;    //包含路径的完整文件名

    public function __construct($formName = 'file', $path='', $allowExt = array('jpg','jpeg','gif','png'),  $maxSize = '2') {

        $this->formName = $formName;
        $this->maxSize = $maxSize;
        $this->allowExt = $allowExt;

        if(!$path) {
            $path = UPLOAD_PATH;
        }

        $times = time();
        $fullpath = trim($path,'/').'/'.date('Ym',$times).'/'.date('d',$times).'/';
        if(!file_exists($fullpath)) {
            mkdir($fullpath, 0777, 1);
        }
        $this->savePath = $fullpath;
    }

    public function upload() {
        $this->getExt();
        $size = $this->getSize();

        if($this->Error) {
            return false;
        }

        $this->info['filename'] = $_FILES[$this->formName]['name'];
        $this->info['savename'] = date('YmdHis').rand(0, 9).'.'.$this->ext;
        $this->info['ext']      = $this->ext;
        $this->info['savepath'] = $this->savePath;
        $this->info['fullname'] = $this->savePath.$this->info['savename'];

        if(move_uploaded_file($_FILES[$this->formName]["tmp_name"], $this->info['fullname'])) {
            return $ture;
        } else {
            $this->Error = 4; //文件保存失败
            return false;
        }
    }

    /**
     * 生成缩略图
     * @return [type] [description]
     */
    public function thumb($width = 120, $height = 120) {
        $thumbPrefix = "thumb_";

        //目标图片地址
        $dst_img = $this->savePath.$thumbPrefix.$this->info['savename'];
        //源图片
        $src_img = $this->info['fullname'];
        $srcinfo = getimagesize($src_img);

        $otfunc = 'image' . ($this->ext == 'jpg' ? 'jpeg' : $this->ext);
        $srcinfo = getimagesize($src_img);
        $src_w = $srcinfo[0];
        $src_h = $srcinfo[1];
        $type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
        $createfun = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);

        $dst_h = $height;
        $dst_w = $width;
        $x = $y = 0;


        if(($width> $src_w && $height> $src_h) || ($height> $src_h && $width == 0) || ($width> $src_w && $height == 0)){
            $proportion = 1;
        }
        if($width> $src_w){
            $dst_w = $width = $src_w;
        }
        if($height> $src_h){
            $dst_h = $height = $src_h;
        }

        if(!$width && !$height && !$proportion){
            return false;
        }
        if(!$proportion){
            if($cut == 0){
                if($dst_w && $dst_h){
                    if($dst_w/$src_w> $dst_h/$src_h){
                        $dst_w = $src_w * ($dst_h / $src_h);
                        $x = 0 - ($dst_w - $width) / 2;
                    } else {
                        $dst_h = $src_h * ($dst_w / $src_w);
                        $y = 0 - ($dst_h - $height) / 2;
                    }
                } else if($dst_w xor $dst_h) {
                    if($dst_w && !$dst_h) {
                        //有宽无高
                        $propor = $dst_w / $src_w;
                        $height = $dst_h  = $src_h * $propor;
                    } else if(!$dst_w && $dst_h) {
                        //有高无宽
                        $propor = $dst_h / $src_h;
                        $width  = $dst_w = $src_w * $propor;
                    }
                }
            } else {
                if(!$dst_h) { //裁剪时无高
                    $height = $dst_h = $dst_w;
                }

                if(!$dst_w) { //裁剪时无宽
                    $width = $dst_w = $dst_h;            
                }

                $propor = min(max($dst_w / $src_w, $dst_h / $src_h), 1);
                $dst_w = (int)round($src_w * $propor);
                $dst_h = (int)round($src_h * $propor);
                $x = ($width - $dst_w) / 2;
                $y = ($height - $dst_h) / 2;
            }
        } else {
            $proportion = min($proportion, 1);
            $height = $dst_h = $src_h * $proportion;
            $width  = $dst_w = $src_w * $proportion;
        }

        $src = $createfun($src_img);
        $dst = imagecreatetruecolor($width ? $width : $dst_w, $height ? $height : $dst_h);
        $white = imagecolorallocate($dst, 255, 255, 255);
        imagefill($dst, 0, 0, $white);

        if(function_exists('imagecopyresampled')) {
            imagecopyresampled($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
        } else {
            imagecopyresized($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
        }
        $otfunc($dst, $dst_img);
        imagedestroy($dst);
        imagedestroy($src);
        return true;

        $this->info['thumb_file'] = $dst_img;
        $this->info['thumb_name'] = $thumbPrefix.$this->info['savename'];
    }

    /**
     * 获取上传信息
     * @return [type] [description]
     */
    public function info() {
        return $this->info;
    }

    /**
     * 获取扩展名
     * @return [type] [description]
     */
    protected function getExt() {   
        $ext = pathinfo($_FILES[$this->formName]['name'], PATHINFO_EXTENSION);
        if(!in_array($ext,$this->allowExt)){
            $this->CanUpload = false;
            $this->Error = 1; //不允许的文件类型　
        }
        $this->ext = $ext;
    }

    /**
     * 获取文件尺寸
     * @return [type] [description]
     */
    protected function getSize() {
        if ($_FILES[$this->formName]['size'] == 0) {
            $this->Error = 3; //文件没有上传
            return false;
        }

        $this->info['size'] = $_FILES[$this->formName]['size'];

        $size = ($_FILES[$this->formName]['size']) / (1024 * 1024);
        if($size > $this->maxSize) {
            $this->Error = 4; //超出最大尺寸
            return false;
        }

        return true;
    }


}